package com.aefottt.struggle_star.bean;

import androidx.annotation.IntDef;

import com.aefottt.struggle_star.util.DatabaseUtil;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;

/**
 * 数据Bean---父类
 */
public class DataBean extends LitePalSupport implements Serializable {
    public static final int IMPORTANT_LEVEL_NONE = -1;
    public static final int IMPORTANT_LEVEL_A = 0;
    public static final int IMPORTANT_LEVEL_B = 1;
    public static final int IMPORTANT_LEVEL_C = 2;
    public static final int IMPORTANT_LEVEL_D = 3;
    @Target(ElementType.FIELD)
    @IntDef({IMPORTANT_LEVEL_NONE, IMPORTANT_LEVEL_A, IMPORTANT_LEVEL_B, IMPORTANT_LEVEL_C, IMPORTANT_LEVEL_D})
    @Retention(RetentionPolicy.SOURCE)
    public @interface ImportantLevel{}

    @Target(ElementType.FIELD)
    @IntDef({0, 1}) // 0表示未完成，1表示完成
    @Retention(RetentionPolicy.SOURCE)
    public @interface IntBoolean{}

    private int listId; // 父类对应的Id
    private final int dataId; // 该数据对应Id
    private String dataTitle; // 数据标题
    private int rank;
    private @IntBoolean int isAchieved; // 是否已完成
    private long createTime; // 数据创建时间
    private long achievedTime; // 数据完成时间
    private long deadTime; // 数据截止时间
    private long reminderTime; // 数据提醒时间
    private @ImportantLevel int importantLevel; // 重要程度，分为A,B,C,D四个等级

    public DataBean(int listId, String dataTitle, int rank, int isAchieved, long createTime,
                    long achievedTime, long deadTime, long reminderTime, int importantLevel) {
        this.listId = listId;
        // 获取最大的Id
        this.dataId = DatabaseUtil.getLatestId(getClass(), "dataId") + 1;
        this.dataTitle = dataTitle;
        this.rank = rank;
        this.isAchieved = isAchieved;
        this.createTime = createTime;
        this.achievedTime = achievedTime;
        this.deadTime = deadTime;
        this.reminderTime = reminderTime;
        this.importantLevel = importantLevel;
    }

    /**
     * 暂时的构造器，不能被保存
     * @param listId 对应的清单ID
     */
    public DataBean(int listId) {
        this.listId = listId;
        // 获取最大的Id
        this.dataId = DatabaseUtil.getLatestId(getClass(), "dataId") + 1;
        importantLevel = IMPORTANT_LEVEL_NONE;
    }

    public DataBean(int listId, int dataId){
        this.listId = listId;
        this.dataId = dataId;
        importantLevel = IMPORTANT_LEVEL_NONE;
    }

    public void setListId(int listId) {
        this.listId = listId;
    }

    public void setDataTitle(String dataTitle) {
        this.dataTitle = dataTitle;
    }

    public void setIsAchieved(int isAchieved) {
        this.isAchieved = isAchieved;
    }

    public void setAchievedTime(long achievedTime) {
        this.achievedTime = achievedTime;
    }

    public void setDeadTime(long deadTime) {
        this.deadTime = deadTime;
    }

    public void setImportantLevel(int importantLevel) {
        this.importantLevel = importantLevel;
    }

    public static int getImportantLevelA() {
        return IMPORTANT_LEVEL_A;
    }

    public static int getImportantLevelB() {
        return IMPORTANT_LEVEL_B;
    }

    public static int getImportantLevelC() {
        return IMPORTANT_LEVEL_C;
    }

    public static int getImportantLevelD() {
        return IMPORTANT_LEVEL_D;
    }

    public int getListId() {
        return listId;
    }

    public int getDataId() {
        return dataId;
    }

    public String getDataTitle() {
        return dataTitle;
    }

    public int getIsAchieved() {
        return isAchieved;
    }

    public long getCreateTime() {
        return createTime;
    }

    public long getAchievedTime() {
        return achievedTime;
    }

    public long getDeadTime() {
        return deadTime;
    }

    public List<LabelBean> getLabels() {
        return LitePal.where("dataId = ?", String.valueOf(dataId)).find(LabelBean.class);
    }

    public int getImportantLevel() {
        return importantLevel;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public long getReminderTime() {
        return reminderTime;
    }

    public void setReminderTime(long reminderTime) {
        this.reminderTime = reminderTime;
    }
}
